package edu.mju.controller;

import edu.mju.entity.TouristCompany;
import edu.mju.entity.User;
import edu.mju.service.ICompanyService;
import edu.mju.service.IUserService;
import edu.mju.service.impl.CompanyServiceImpl;
import edu.mju.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author wyp
 */
@WebServlet("/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String loginName=req.getParameter("userName");
        String loginPwd=req.getParameter("passWord");
        String capche = req.getParameter("inputCapche");
        //获取生成的验证码
        String capCode = (String) req.getSession().getAttribute("capcode");
        String str;

        //如果用户名是学号，密码是1234，正确显示你好，如果错误，显示账号或密码错误
        if(capCode != null) {
            if (capCode.equals(capche)) {
                IUserService userService = new UserServiceImpl();
                User user = userService.login(loginName,loginPwd);
                if(user!=null){
                    req.getSession().setAttribute("user", loginName);
                    req.getSession().setAttribute("userId", user.getUserId());
                    req.getSession().setAttribute("userRole",user.getUserRole());
                    if("tourAdministration".equals(user.getUserRole())){
                        resp.sendRedirect("./backindex");
                    }else if("tourCompany".equals(user.getUserRole())){
                        ICompanyService service = new CompanyServiceImpl();
                        TouristCompany company = service.findByuserId(user.getUserId());
                        req.getSession().setAttribute("companyId", company.getCompanyId());
                        req.getSession().setAttribute("companyName", company.getCompanyName());
                        resp.sendRedirect("./companyindex");
                    } else if("tourist".equals(user.getUserRole())){
                        resp.sendRedirect("./index.jsp");
                    }
                }else {
                    loginFail(req,resp,"账号或密码错误");
                }
            }else {
                loginFail(req,resp,"验证码错误");
            }
        }else {
            loginFail(req,resp,"验证码未生成");
        }
    }

    private void loginFail(HttpServletRequest req,HttpServletResponse resp,String str) throws ServletException, IOException {
        req.getRequestDispatcher("./login/loginError.jsp?err="+ URLEncoder.encode(str,"UTF-8")).forward(req,resp);
    }

}
